package com.ciaojian.worker.service;

import cn.hutool.core.util.ObjectUtil;
import com.ciaojian.core.enums.business.OrderOrderTypeEnum;
import com.ciaojian.core.mapper.OrderCommissionMapper;
import com.ciaojian.core.mapper.OrderMapper;
import com.ciaojian.core.model.OrderCommission;
import com.ciaojian.core.model.vo.WorkerSalaryVo;
import com.ciaojian.core.service.OrderCommissionService;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author oxiny
 */
@Service
public class OrderCommissionServiceImpl extends OrderCommissionService {
    @Resource
    private OrderCommissionServiceImpl orderCommissionService;
    @Resource
    private OrderServiceImpl orderService;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderCommissionMapper orderCommissionMapper;

    public WorkerSalaryVo getsalary(Integer workerId, Date beginDate, Date endDate){
        WorkerSalaryVo workerSalaryVo = new WorkerSalaryVo();

        // 本月累计业绩
        Integer sumAmount = orderCommissionService.getBaseMapper().sumAmount(workerId, new Integer[]{0, 3}, beginDate, endDate);

        BigDecimal performance = new BigDecimal(sumAmount);
        performance = performance.multiply(new BigDecimal(0.01));
        performance = performance.setScale(2,BigDecimal.ROUND_HALF_UP);

        // 抖音业绩 双倍
        BigDecimal dyPerformance =orderCommissionMapper.dyPerformanceDouble(workerId,beginDate,endDate);

        //总业绩
        BigDecimal amount = dyPerformance.add(performance);
        //业绩达标率
        BigDecimal ratio = amount.divide(new BigDecimal(20000));
        ratio = ratio.multiply(new BigDecimal(100));
        int perforRatio = ratio.intValue();
        if (perforRatio>=90){
            // 业绩提成
            amount = amount.divide(new BigDecimal(20000));
            amount = amount.multiply(new BigDecimal(800));
            amount = amount.setScale(2,BigDecimal.ROUND_HALF_UP);
        }else {
            amount = new BigDecimal(0);
        }



        // 会员订单(新/老)
        Map<String, Integer> newAndOldMemberNum = orderMapper.countNewAndOldMember(null, workerId, beginDate, endDate);

        //套餐提成
        BigDecimal memberAmount = orderCommissionMapper.getWorkerCommiss(workerId,beginDate,endDate);

        //会员提成个数
        workerSalaryVo.setAmount(amount);
        workerSalaryVo.setPerformance(performance);
        workerSalaryVo.setDyPerformance(dyPerformance);

        Integer memberNum = orderCommissionMapper.getMemberNum(workerId, beginDate, endDate);
        if (memberNum>=60 && memberNum<80){
            workerSalaryVo.setMemberSell(300);
        }else if(memberNum>=80 && memberNum<100){
            workerSalaryVo.setMemberSell(600);
        }else if(memberNum>=100 && memberNum<120){
            workerSalaryVo.setMemberSell(1000);
        }else if(memberNum>=120){
            workerSalaryVo.setMemberSell(1500);
        }else {
            workerSalaryVo.setMemberSell(0);
        }

        if (ObjectUtil.isNull(newAndOldMemberNum)){
            workerSalaryVo.setNewAndOldMemberNum(null);
        }else {
            workerSalaryVo.setNewAndOldMemberNum(newAndOldMemberNum);
        }

        workerSalaryVo.setMemberAmount(memberAmount);
        workerSalaryVo.setMemberNum(memberNum);
        workerSalaryVo.setPerforRatio(perforRatio);


        return workerSalaryVo;
    }
}
